from ..utils.crypto_utils import server_utils
from ..utils.logger import logger
from bs4 import BeautifulSoup


class BackendTeachingAssist:

    @staticmethod
    @server_utils.handle_exceptions("教辅-查询未使用的教辅编码失败")
    def find_teaching_assistant_id():
        logger.info("------教辅_获取未使用的教辅编码------")
        data_dict = {
            'teachingAssistantId': "189",
            'isDownloadTeachingAssistantCode': "7",
            'status': '1',
            'bindStatus': "0",
            'useStatus': '0',
            'tacfirstId': "-1",
            'tacSecondId': "-1",
            'tacThirdId': "-1",
        }

        html_content = server_utils.post_request(url=f"/teachingAssistantCode/list.html?refreshTeachingAssistantId=189", data=data_dict)

        soup = BeautifulSoup(html_content, 'html.parser')

        # 查找所有的教辅编码所在的表格行
        rows = soup.find_all('tr')[1:]  # 跳过表头

        # 提取教辅编码的值
        codes = []
        for row in rows:
            # 找到包含教辅编码的单元格
            code_cells = row.find_all('td')
            if len(code_cells) > 0:
                # 获取教辅编码所在的第一个单元格
                code = code_cells[0].text.strip()
                codes.append(code)

        return codes

    @staticmethod
    @server_utils.handle_exceptions("教辅-删除用户绑定的教辅失败")
    def delete_teaching_assistant_user_relation(code, userInfoId):
        logger.info("------删除用户绑定的教辅------")
        data_dict = {
            'code': code,
            'userInfoId': userInfoId,
        }
        return server_utils.post_request(url=f"/teachingAssistantsWhiteList/deleteTeachingAssistantUserRelation.html", data=data_dict)


backend_teaching_assist = BackendTeachingAssist()
